package com.avic.szzx.controller;

import com.alibaba.fastjson.JSONObject;
import com.avic.szzx.common.BaseResponse;
import com.avic.szzx.common.RequestObj;
import com.avic.szzx.controller.bean.NebulaResult;
import com.avic.szzx.controller.bean.quality_tracing.EquipmentInformationCorrespondence;
import com.avic.szzx.controller.bean.quality_tracing.ProcessRecord;
import com.avic.szzx.controller.bean.quality_tracing.relationship.EquipmentProcessRelationship;
import com.avic.szzx.service.BusinessProcessingService;
import com.avic.szzx.service.NebulaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class EquipmentController {
    @Autowired
    private BusinessProcessingService businessProcessingService;

    @Autowired
    private NebulaService nebulaService;

    @PostMapping("/equipment")
    @ResponseBody
    public BaseResponse insertEquipmentData(@RequestBody RequestObj<List<EquipmentInformationCorrespondence>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_information_correspondence";
        List<EquipmentInformationCorrespondence> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.writeEquipmentInfoCorrespondence(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据写入成功！", result);
    }
    @PostMapping("/equipments")
    @ResponseBody
    public BaseResponse insertEquipmentDatas(@RequestBody RequestObj<List<EquipmentInformationCorrespondence>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_information_correspondence";
        List<EquipmentInformationCorrespondence> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.writeEquipmentInfoCorrespondences(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据写入成功！", result);
    }
    @PostMapping("/processRecord")
    @ResponseBody
    public BaseResponse insertProcessRecordData(@RequestBody RequestObj<List<ProcessRecord>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("加工记录实例数据写入失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "processing_record";
        List<ProcessRecord> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.writeProcessRecord(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("加工记录信息实例数据写入失败！");
        }
        return BaseResponse.getInstence().createBySuccess("加工记录信息实例数据写入成功！", result);
    }
    @GetMapping("/graphData")
    @ResponseBody
    public BaseResponse getGraphData(@RequestParam String type,String name) {
        JSONObject nebulaResult = null;
        if (type.equals("VERTEX")) {
            nebulaResult = nebulaService.selectVertexData(name);
        } else {
            nebulaResult = nebulaService.selectEdgeData(name);
        }
        return BaseResponse.getInstence().createBySuccessMessage(nebulaResult);
    }
    @GetMapping("/exploreData")
    @ResponseBody
    public BaseResponse getExploreData(@RequestParam String value,int layer) {
        JSONObject result = nebulaService.selectExploreData(value,layer);
        return BaseResponse.getInstence().createBySuccessMessage(result);
    }
    @GetMapping("/graphDataCount")
    @ResponseBody
    public BaseResponse getGraphDataCount(@RequestParam String type,String name) {
        JSONObject nebulaResult = null;
        if (type.equals("VERTEX")) {
            nebulaResult = nebulaService.selectVertexDataTotal(name);
        } else {
            nebulaResult = nebulaService.selectEdgeDataTotal(name);
        }
        return BaseResponse.getInstence().createBySuccessMessage(nebulaResult);
    }

    @PutMapping("/equipment")
    @ResponseBody
    public BaseResponse updateEquipmentData(@RequestBody RequestObj<List<EquipmentInformationCorrespondence>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_information_correspondence";
        List<EquipmentInformationCorrespondence> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.updateEquipmentInfoCorrespondence(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据写入成功！", result);
    }

    @PostMapping("/equipmentProcess")
    @ResponseBody
    public BaseResponse insertEquipmentProcess(@RequestBody RequestObj<List<EquipmentProcessRelationship>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_process_relationship";
        List<EquipmentProcessRelationship> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.writeEquipmentProcessRelationship(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据写入失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据写入成功！", result);
    }

    @DeleteMapping("/equipment")
    @ResponseBody
    public BaseResponse deleteEquipmentData(@RequestBody RequestObj<List<EquipmentInformationCorrespondence>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据删除失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_information_correspondence";
        List<EquipmentInformationCorrespondence> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.deleteEquipmentInfoCorrespondence(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据删除失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据删除成功！", result);
    }
    @DeleteMapping("/processingRecord")
    @ResponseBody
    public BaseResponse deleteProcessingRecord(@RequestBody RequestObj<List<ProcessRecord>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据删除失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "processing_record";
        List<ProcessRecord> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.deleteProcessRecord(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("加工记录信息实例数据删除失败！");
        }
        return BaseResponse.getInstence().createBySuccess("加工记录信息实例数据删除成功！", result);
    }
    @DeleteMapping("/equipmentProcess")
    @ResponseBody
    public BaseResponse deleteEquipmentProcess(@RequestBody RequestObj<List<EquipmentProcessRelationship>> data) {
        boolean result = false;
        if (data == null) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据删除失败！");
        }
        Boolean status = data.getAutoGraphDB();
        String type = data.getType();
        String tag = "equipment_process_relationship";
        List<EquipmentProcessRelationship> bodys = data.getBody();
        if (null != bodys) {
            result = businessProcessingService.deleteEquipmentProcessRelationship(status, type, tag, bodys);
        }
        if (!result) {
            return BaseResponse.getInstence().createBySuccessMessage("装备配套信息实例数据删除失败！");
        }
        return BaseResponse.getInstence().createBySuccess("装备配套信息实例数据删除成功！", result);
    }
}
